<?php
	class ReponseTxtTrousPositionManager{
		
		private $_db;
	
		public function __construct($db){ // Constructeur
			$this->setDb($db);
		}
		  
		public function setDb(PDO $db){
			$this->_db = $db;
		}	
		
		public function add(ReponseTxtTrousPosition $reponseTxtTrousPosition){// permet d'ajouter un mot
			$q = $this->_db->prepare('INSERT INTO txt_trous SET idQuestion = :idQuestion, idReponse = :idReponse, numMot = :numMot');
			$q->bindValue(':idQuestion', $reponseTxtTrousPosition->idQuestion());
			$q->bindValue(':idReponse', $reponseTxtTrousPosition->idReponse());
			$q->bindValue(':numMot', $reponseTxtTrousPosition->numMot());
					
			$q->execute();
		}
		  
		public function delete(ReponseTxtTrousPosition $reponseTxtTrousPosition){// permet de supprimer un mot
			$this->_db->exec('DELETE FROM txt_trous WHERE idQuestion = '.$reponseTxtTrousPosition->idQuestion());
		}
		  
		public function update(ReponseTxtTrousPosition $reponseTxtTrousPosition){// permet de modifier un mot
			$q = $this->_db->prepare('UPDATE txt_trous SET idRQuestion = :idQuestion, idReponse = :idReponse, numMot = :numMot WHERE id = '.$reponseTxtTrousPosition->id());
			$q->bindValue(':idQuestion', $reponseTxtTrousPosition->idQuestion());
			$q->bindValue(':idReponse', $reponseTxtTrousPosition->idReponse());
			$q->bindValue(':numMot', $reponseTxtTrousPosition->numMot());

			$q->execute();
		}		
		
		public function getByQuestion($idQuestion){// retourne un tableau des mot correspondant à un txt à trous.
			$reponse = array();
			$q = $this->_db->query('SELECT * FROM txt_trous WHERE idQuestion = '.$idQuestion.' ORDER BY numMot');
			while ($donnees = $q->fetch()){
				$reponse[] = new ReponseTxtTrousPosition($donnees);
			}
			return $reponse;
		}		
		
		public function check($idQuestion, $idReponse, $numMot){// retourne l'objet réponse d'id correspondant
			$q = $this->_db->query('SELECT * FROM txt_trous WHERE idQuestion = '.$idQuestion.' AND idReponse = '.$idReponse.' AND numMot = '.$numMot);
			if($q->fetch() != NULL){
				return true;
			} else {
				return false;
			}
		}
		
		public function existe($idQuestion, $idReponse){// retourne l'objet réponse d'id correspondant
			$q = $this->_db->query('SELECT * FROM txt_trous WHERE idQuestion = '.$idQuestion.' AND idReponse = '.$idReponse);
			if($q->fetch() != NULL){
				return true;
			} else {
				return false;
			}
		}
		
		public function getReponseFausse($idQuestion){// retourne une réponse fausse
			$q = $this->_db->query('SELECT * FROM reponse WHERE idRefQuestion = '.$idQuestion);
			while($donnees = $q->fetch()){
				if(!$this->existe($idQuestion, $donnees['id']))
				{
					return new Reponse($donnees);
				}
			}
			return false;
		}		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
?>